--[[NormalFuncForQuest.lua]]

--#include "data\config\quest\MiscQuest.lua" once	--任务相关
--#include "data\config\GlobalConfig.lua" once
--#include "data\functions\player.lua" once

--[[
    函数名称：continueQuestTalk
    函数描述：继续与NPC进行任务对话，如果没有任务状态则关闭对话窗口 
    参数 sysarg 的描述：
]]

QuestCheckFunc = {}
QuestAcceptFunc = {}
QuestFinishFunc = {}
QuestGiveUpFunc = {}

-------------------------------- 以下是任务配置中公共接口，通过字符串匹配
---------接任务检查相关函数
QuestCheckFunc["OnAddItem"] = function( sysarg, qid, itemId, itemNum )
	if Actor.canAddItem(sysarg, itemId, itemNum) then
		return true,0
	else
		return false, Lang.ScriptTips.n00008                                  -- 在判断限制条件后的需要显示的字符串
	end
end

QuestCheckFunc["CheckInTeam"] = function( sysarg, qid, teamerCount )
	local teamId = Actor.getTeamId(sysarg)
	if teamId > 0 then
		local teamTab =  LuaHelp.getTeamMemberList(sysarg)
		if teamTab and #teamTab >= (teamerCount or 2) then
			return true, 0
		end
		if TeamFun.getTeamCaptain(teamId) ~= sysarg then
			return false, Lang.ScriptTips.c000070
		end
	end
	return false, Lang.ScriptTips.c000069
end

QuestCheckFunc["ClrQstTypeCnt"] = function( sysarg, qid, typeId )
	if typeId then
		Actor.clearQuestTypeCount( sysarg, typeId)
	end
	return true, 0
end

---------以下是接受任务的回调
QuestAcceptFunc["OnPromCampCircle"] = function( sysarg, qid, acceptList )
	if acceptList and type(acceptList) == "table" then
		local questId = acceptList[math.random(1,#acceptList)]
		if questId then
			Actor.addQuest(sysarg, questId)
		end
	end
end

--护送
QuestAcceptFunc["OnPromEscort"] = function( sysarg, qid, monsterId )	
	local x,y = Actor.getEntityPosition(sysarg, 0,0)
	local monsterPtr = Actor.createMonster(sysarg, monsterId, x, y, 60*1000)
	if monsterPtr then
		local dVar = Actor.getDyanmicVar(sysarg)
		Actor.followActor(monsterPtr, sysarg, 1, 30, true) --跟随某个玩家
		Actor.setEntityTelByOwnderAttr(monsterPtr, true)   --设置实体是否跟着归属者传送
		Actor.setEntityLazyFollow(monsterPtr, true)        --设置实体是否惰性跟随
		Actor.setDenyAttackedByActor(monsterPtr, true)     --设置实体是否不可被玩家攻击		
		Actor.refreshActorObserveList(sysarg)     --刷新观察者，防止客户端延迟 
		dVar.escortMonsterHandle = Actor.getHandle(monsterPtr)
	end
end

QuestAcceptFunc["OnPromAddItem"] = function( sysarg, qid, itemId, quaility, strong, itemNum, bind )
	Actor.addItem(sysarg, itemId, quaility, strong, itemNum, bind)
end

QuestAcceptFunc["OnPromTelport"] = function( sysarg, qid, sceneName, xPos, yPos)
	System.telportSceneByName(sysarg,sceneName, xPos, yPos)             --将玩家传送到交任务的NPC前
end

--接任务后，进入副本
QuestAcceptFunc["OnPromEnterFuben"] = function( sysarg, qid, fubenId )
	if fubenId == SbkFubenConfig[1].fubenId then					
		EnterSbkFuben( sysarg )									--SBK体验副本
	elseif fubenId == StoneTombPigConfigForExp.fubenId then
		EnterStoneTombPig( sysarg, 1 )							--体验模式进入石墓烧猪副本(体验模式与正式模式的配置不一样)
	else
		OnCommonQuestFubenEnter( sysarg, fubenId )
	end
end

QuestAcceptFunc["OnPromStrong"] = function( sysarg, qid, strongLv, targetQid, targetId )
	--判断是否有物品的强化值达到了
	if Item.isStrongLevel(sysarg, strongLv) then	--判断是否有物品的强化值达到了目标了
		Actor.setQuestValue(sysarg, targetQid, targetId,1)	--设置任务的目标值，30004和1都是target里面配置的值
	end
end

QuestAcceptFunc["OnPromItemHoleCount"] = function( sysarg, qid, holeCount, targetQid, targetId )
	if Item.isHoleItemCount(sysarg, holeCount) then	--判断是否有物品的镶嵌
		Actor.setQuestValue(sysarg, targetQid, targetId, 1)	--设置任务的目标值，30004和1都是target里面配置的值
	end
end

QuestAcceptFunc["OnPromItemForge"] = function( sysarg, qid, targetQid, targetId )
	if Item.hasForgeItem(sysarg) then	--判断是否有物品精锻
		Actor.setQuestValue(sysarg,targetQid, targetId, 1)	--设置任务的目标值，30004和1都是target里面配置的值
	end
end

QuestAcceptFunc["OnPromSuitEquip"] = function( sysarg, qid, suitAId, suitACount, suitBId, suitBCount )
	if Item.isGetAllSuitEquip(sysarg, suitAId, suitACount) then
		Actor.setQuestValue(sysarg, qid, suitAId, suitACount)
	end
	if suitBId and suitBCount then
		if Item.isGetAllSuitEquip(sysarg, suitBId, suitBCount) then
			Actor.setQuestValue(sysarg, qid, suitBId, suitBCount)
		end
	end
end

QuestAcceptFunc["OnPromHasEquiment"] = function( sysarg, qid, maleItems, femaleItems, targetId )
	local job = Actor.getIntProperty(sysarg,PROP_ACTOR_VOCATION)
	local sex = Actor.getIntProperty(sysarg,PROP_ACTOR_SEX)
    local Itemid = femaleItems[job]
	if sex == 0 then
		Itemid = maleItems[job]
	end
	if Itemid then
		if Actor.hasEquiment(sysarg,Itemid) then
			Actor.setQuestValue(sysarg, qid, targetId, 1)
		end
	end
end

QuestAcceptFunc["OnPromBagHoleCount"] = function( sysarg, qid, count, tagetQid, targetId )
	if Item.isHoleCount(sysarg, count) then	--判断是否有物品的打孔
		Actor.setQuestValue(sysarg, tagetQid, targetId,1)	--设置任务的目标值，30004和1都是target里面配置的值
	end
end

QuestAcceptFunc["OnPromCurEquipedGem"] = function( sysarg, qid, targetId )
	if GemItem.getCurrEquipedGem(sysarg) ~= nil then
		Actor.setQuestValue(sysarg, qid, targetId, 1)
	end
end

QuestAcceptFunc["OnPromBattlePet"] = function( sysarg, qid, targetId )
	if Pet.getBattlePetId(sysarg) >= 0 then
		Actor.setQuestValue(sysarg, qid, targetId, 1)
	end
end

QuestAcceptFunc["OnPromHasAchieve"] = function( sysarg, qid, achieveId, targetId )
	if Actor.isAchieveFinished(sysarg, achieveId) then
		Actor.setQuestValue(sysarg, qid, targetId, 1)
	end
end

QuestAcceptFunc["OnPromPlayVideo"] = function( sysarg, qid, playerId, thisNPC )
	StartPlayVideo(sysarg, playerId)
	--点击npc,让用户看完动画后可以看到对话框
	--Actor.npcTalk(sysarg,thisNPC)  (没用了)
end

QuestAcceptFunc["OnPromCreateMonster"] = function( sysarg, qid, mondId, xPos, yPos, liveTime )
	Actor.createMonster(sysarg, mondId, xPos, yPos, liveTime)   --ID，坐标，存活时间     
end

QuestAcceptFunc["OnPromFly"] = function( sysarg, qid, vId, pathId, flyType )
	if vId and pathId then
		Actor.onBoard(sysarg, vId, pathId, flyType)
	end
end

QuestAcceptFunc["OnPromRide"] = function( sysarg, qid, rideId, layerId, rideSecs )
	local rideSecs = rideSecs or 60
	if rideId and layerId then
		Actor.addBuff(sysarg, 92, 0, rideId, 1, rideSecs, Lang.ScriptTips.c000080,false, nil,layerId)
	end
end

QuestAcceptFunc["OnPromWorldTalk"] = function( sysarg, qid, index )
	local tabLen = #QuestWorldTalk
	if not index and index > tabLen then
		index = math.random(1, tabLen)
	end
	local talkStr = QuestWorldTalk[index]
	Actor.sendChat( sysarg, ciChannelWorld, talkStr, false)
end

QuestAcceptFunc["OnPromPlayMovie"] = function( sysarg, qid, movieId)
	if movieId then
		Actor.triggerPictureGuide(sysarg, movieId)
	end
end

QuestAcceptFunc["OnPromAddBuff"] = function( sysarg, qid, buffId)
	if buffId then
		Actor.addBuffById(sysarg, buffId)
	end
end

--------完成时回调相关函数
QuestFinishFunc["OnCompPlayMovie"] = function( sysarg, qid, movieId )
	if movieId then
		Actor.triggerPictureGuide(sysarg, movieId)
	end
end

QuestFinishFunc["OnKillPlayer"] = function( sysarg, qid )
	local campid = Actor.getCampId(sysarg) 
	qid = tonumber(qid)

	local award = SpyCampAwardConfig
	local level = Actor.getIntProperty(sysarg,PROP_CREATURE_LEVEL)
	local renown = award.renown
	local zycont = award.zycont
	local exp = award.exp[level]
	if  nil then  --判断是否在发布刺探阵营期间 
		----print("in the spy time!!")
		renown = renown + math.floor(renown*0.3)
		zycont = zycont + math.floor(zycont*0.3)
		exp = exp + math.floor(exp*0.3)
	end                    
	Actor.addExp(sysarg,exp,200,0)     --经验奖励 

	--Actor.giveAward(sysarg,qatRenown,0,renown,0, 0, 1, 0,602,"spycamp")
	Actor.changeZYCount(sysarg,zycont,"spycamp")                                   --阵营贡献值奖励                   

	for i = 1, #CampSpyQuest do
		local data = CampSpyQuest[i]
		Actor.setQuestValue(sysarg,data.qid,data.id,data.count)
	end
end

QuestFinishFunc["OnCompCampCircle"] = function( sysarg, qid, acceptList, circleType, questId )
	Actor.triggerQuestEvent(sysarg, 38, circleType, 1)
	if Actor.hasQuest(sysarg, questId) then
		if acceptList and type(acceptList) == "table" and not Actor.canFinishQuest(sysarg, questId) then
			local questId = acceptList[math.random(1,#acceptList)]
			if questId then
				Actor.addQuest(sysarg, questId)
			end
		end
	end
end

QuestFinishFunc["OnCompEscort"] = function( sysarg, qid, monsterId )
	local dVar = Actor.getDyanmicVar(sysarg)
	if dVar.escortMonsterHandle then
		Fuben.clearMonster(dVar.escortMonsterHandle)
		dVar.escortMonsterHandle = nil
	end
end

QuestFinishFunc["OnFinishCircle"] = function( sysarg, qid, monsterId )
	Actor.triggerQuestEvent(sysarg, 127, 31142, 1)
end

QuestFinishFunc["OnFinRemItem"] = function( sysarg, qid, itemId, itemNum )
	if Actor.isFinishQuest(sysarg, qid) then
    	Actor.removeItem(sysarg, itemId, itemNum)
    end
end

QuestFinishFunc["OnFinLeanrSkill"] = function( sysarg, qid, skills )
	if Actor.isFinishQuest(sysarg, qid) then
		local jobId = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
		local skillId = skills[jobId]
		if skillId then
			Actor.learnSkill(sysarg, skillId, 1)
		end
	end
end

QuestFinishFunc["OnFinCamp"] = function( sysarg, qid )
	local campid = Actor.getCampId(sysarg)
	local award = SpyCampAwardConfig
	local level = Actor.getIntProperty(sysarg,PROP_CREATURE_LEVEL)
	local renown = award.renown
	local zycont = award.zycont
	local exp = award.exp[level]
	if  nil then  --判断是否在发布刺探阵营期间 
		----print("in the spy time!!")
		renown = renown + math.floor(renown*0.3)
		zycont = zycont + math.floor(zycont*0.3)
		exp = exp + math.floor(exp*0.3)
	end                    
	Actor.addExp(sysarg,exp,200,0)     --经验奖励 

	--Actor.giveAward(sysarg,qatRenown,0,renown,0, 0, 1, 0,602,"spycamp")
	Actor.changeZYCount(sysarg,zycont,"spycamp")                                   --阵营贡献值奖励                   

	for i = 1, #CampSpyQuest do
		local data = CampSpyQuest[i]
		Actor.setQuestValue(sysarg,data.qid,data.id,data.count)
	end
end

QuestFinishFunc["OnPlayerEffect"] = function( sysarg, qid, effectIdx )
	local cfgSize = #QstFinEffect
	if not effectIdx or effectIdx == 0 or effectIdx > cfgSize then
		effectIdx = math.random(1, cfgSize)
	end
	local curTab = QstFinEffect[effectIdx]
	if curTab then
		for k, effect in ipairs(curTab) do
			Actor.addSceneEffect(sysarg,effect.effectId,effect.effectType, effect.durTime, effect.nDelayTime,effect.x,effect.y) --添加特效 
		end
	end
end

QuestFinishFunc["OnFinLoadRes"] = function( sysarg, qid, resId )
	if not resId then
		return
	end
	local netPack = DataPack.allocPacket(sysarg, 6, 12)  --申请一个数据包
	if netPack then
		 DataPack.writeInt(netPack, resId)
		 DataPack.flush(netPack)
	end  
end

---------放弃任务回调
QuestGiveUpFunc["OnGvupRemItem"] = function( sysarg, qid, itemId )
	local qitemcount = Actor.getItemCount(sysarg, itemId) 
	if qitemcount >= 1 then 
		Actor.removeItem(sysarg, itemId,qitemcount)
	end
end
